package com.glory.dgcpcpda.activity.check;


import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.entity.CheckSheetEntity;
import com.glory.dgcpcpda.entity.MLotToolEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import butterknife.BindView;
import butterknife.OnClick;

public class CheckInspectionActivity extends BaseActivity {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvState)
    TextView tvState;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.tvEqpType)
    TextView tvEqpType;
    @BindView(R.id.tvStep)
    TextView tvStep;
    @BindView(R.id.tvCheckTime)
    TextView tvCheckTime;
    @BindView(R.id.tvClazz)
    TextView tvClazz;
    @BindView(R.id.tvlotID)
    TextView tvlotID;
    @BindView(R.id.tvCustomCode)
    TextView tvCustomCode;
    @BindView(R.id.tvPartName)
    TextView tvPartName;
    @BindView(R.id.tvPackageType)
    TextView tvPackageType;
    @BindView(R.id.tvEqpSubState)
    TextView tvEqpSubState;
    @BindView(R.id.tvEqpComClass)
    TextView tvEqpComClass;
    @BindView(R.id.tvEqpState)
    TextView tvEqpState;
    @BindView(R.id.tvCheckName)
    TextView tvCheckName;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private CheckSheetEntity checkSheetEntity = new CheckSheetEntity();
    private String mObjrctRRN = "";
    private static final String Start  = "Start";
    private Boolean IsStart = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_new_check_table;
    }

    @Override
    protected void initView() {
        tvTitle.setText("巡检");
        tvBaseSubmit.setText("下一步");
        tvState.setVisibility(View.VISIBLE);
        setMainLayout(llMain);
        String eqpid = getIntent().getExtras().getString("EQPID");
        mObjrctRRN = getIntent().getExtras().getString("OBJECTRRN");
        if (eqpid!= null){
            etEqpWork.setText(eqpid);
            requestCheckInfo(eqpid, "");
        }
        initEdit(etEqpWork, s -> requestCheckInfo(s, ""));
    }

    @Override
    protected void initData() {
    }

    public void requestCheckInfo(String s, String action){
        FreeUI_EntityCallBack<BaseResponseBean<CheckSheetEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<CheckSheetEntity>>
                (new TypeToken<BaseResponseBean<CheckSheetEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<CheckSheetEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    checkSheetEntity = response.body().getResponse().getBody();
                    tvCustomCode.setText(checkSheetEntity.getCUSTOMERCODE());
                    tvEqpComClass.setText(checkSheetEntity.getEQPCOMCLASS());
                    tvEqpState.setText(checkSheetEntity.getEQPSTATE());
                    tvEqpSubState.setText(checkSheetEntity.getEQPSUBSTATE());
                    tvlotID.setText(checkSheetEntity.getLOTID());
                    tvPackageType.setText(checkSheetEntity.getPACKAGETYPE());
                    tvPartName.setText(checkSheetEntity.getPARTNAME());
                    tvClazz.setText(checkSheetEntity.getSHIFT());
                    tvEqpType.setText(checkSheetEntity.getEQUIPMENTTYPE());
                    tvCheckTime.setText(checkSheetEntity.getSHEETTIME());
                    if(checkSheetEntity.getSHEETLIST().size() > 0){
                        tvCheckName.setText(checkSheetEntity.getSHEETLIST().get(0).getNAME());
                        tvStep.setText(checkSheetEntity.getSHEETLIST().get(0).getSTEPNAME());
                    }

                    if(Start.equals(action)){
                        requestTrackinLogEvent();
                        return;
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<CheckSheetEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"CHECKSHEET");
            }
        };

        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QUERY");
        map.addProperty("EQUIPMENTID",s);
        map.addProperty("CHECKSHEETDATARRN",mObjrctRRN);
        map.addProperty("CHECKTIMING","Daily_Inspect");
        NetUtils.requestPostNet("CPC.CHECKSHEET","CHECKSHEET", CheckInspectionActivity.this,map,callBack);
    }

    // 巡检 目测现在没有 改变设备状态 需要他点开始就好
    public void requestTrackinLogEvent(){
        FreeUI_EntityCallBack<BaseResponseBean<MLotToolEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<MLotToolEntity>>
                (new TypeToken<BaseResponseBean<MLotToolEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<MLotToolEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())){
                    showToast("操作成功",false);

                    IsStart = true;
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<MLotToolEntity>> response) {
                super.onError(response);
                loadError(response.getException(), "TRACKINLOGEVENT");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE", "TRACKINLOGEVENT");
        map.addProperty("EQUIPMENTID", getText(etEqpWork));
        map.addProperty("COMMENT", "巡检");
        NetUtils.requestPostNet("CPC.TRACKINLOGEVENT", "TRANSFERMES",this, map, callBack);
    }


    @OnClick({R.id.tvBaseSubmit,R.id.tvState})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (actionControl("NewCheck2")){
                    return;
                }

                if (!IsStart){
                    showToast("当前设备未开始",false);
                    return;
                }

                if (checkSheetEntity.getSHEETLIST().size() <= 0){
                    showToast("未获取到点检数据",false);
                    return;
                }

                Bundle bundle = new Bundle();
                bundle.putSerializable("SHEETDATA", checkSheetEntity);
                bundle.putString("OBJECTRRN",mObjrctRRN);
                bundle.putString("CHECKTIME","巡检");
                bundle.putString("EQUIPMENTID",getText(etEqpWork));
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this,CheckDescActivity.class);
                finish();
                break;
            case R.id.tvState:
                if (getText(etEqpWork).equals("")){
                    showToast("设备号不能为空",false);
                    return;
                }
                requestCheckInfo(getText(etEqpWork), Start);
                break;
        }
    }


}
